package com.itxiaowu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itxiaowu.dao.MemberDao;
import com.itxiaowu.pojo.Member;
import com.itxiaowu.service.MemberService;
import com.itxiaowu.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 查找会员
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {

        return memberDao.findByTelephone(telephone);

    }

    /**
     * 添加会员信息
     *
     * @param member
     */
    @Override
    public void addMember(Member member) {
        String password = member.getPassword();
        if( password != null){
            // 进行密码加密
            System.out.println("加密前: " + password);

            String md5Password = Md5Utils.md5(password);

            System.out.println("加密后的结果是:" + md5Password);
        }

        memberDao.insertMember(member);
    }

    /**
     * 使用日期查询数据库  获得会员人数
     *
     * @param list
     * @return
     */
    @Override
    public List<Integer> getMemberReport(List<String> list) {
        //1: 准备数据
        List<Integer> countList = new ArrayList<>();
        if(list != null && list.size() > 0) {
            for (String date : list) {
                date = date + "-31";

                System.out.println(date);

                //2: 调用方法查询
                Integer count = memberDao.getMemberReport(date);
                //3: 封装返回数据
                countList.add(count);
            }
        }

        System.out.println("个数是：" + countList.size());
        return countList;
    }

    /**
     * 查询性别人数占比
     * @return
     */
    @Override
    public List<Map<String, Object>> getMemberSexReport() {

        return memberDao.getMemberSexReport();
    }

    /**
     * 查询年龄段人数占比
     * @return
     */
    @Override
    public Map<String, Object> getMemberBirthdayReport() {

        Map<String,Object>map1=new HashMap<>();
        map1.put("name","0-18岁");
        map1.put("value",memberDao.findBy018());
        Map<String,Object>map2=new HashMap<>();
        map2.put("name","18-30岁");
        map2.put("value",memberDao.findBy1830());
        Map<String,Object>map3=new HashMap<>();
        map3.put("name","30-45岁");
        map3.put("value",memberDao.findBy3045());
        Map<String,Object>map4=new HashMap<>();
        map4.put("name","45岁以上");
        map4.put("value",memberDao.findByAfter45());
        Map<String,Object>map5=new HashMap<>();
        map5.put("name","未知年龄");
        map5.put("value",memberDao.findByNull());

        List<Map<String, Object>>list=new ArrayList<>();
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        Map<String,Object> map = new HashMap<>();
        map.put("birthdayCount",list);

        List<String> birthday = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : list) {
            String name = (String) stringObjectMap.get("name");
            birthday.add(name);
        }
        map.put("birthday",birthday);
        return map;
    }

    /**
     * 根据月份获得会员数据
     *
     * @param list
     * @return
     */
    @Override
    public List<Integer> getMemberReportByMonth(List<String> list) {

        //1: 准备数据
        Map<String,String> map = new HashMap<String,String>();
        String date1 = "";
        String date2="";
        List<Integer> countList = new ArrayList<>();
        if(list != null && list.size() > 0) {
            for (String date : list) {
                date1 = date + "-31";
                System.out.println(date);
                date2 = date+"-1";
                map.put("date1",date1);
                map.put("date2",date2);
                //2: 调用方法查询
                Integer count = memberDao.getMemberReportByMonth(map);
                //3: 封装返回数据
                countList.add(count);
            }
        }

        System.out.println("个数是：" + countList.size());
        return countList;
    }


}
